import { FC, memo, useRef, useState } from 'react'
import {
  addNewOrderProject,
  getAllNewProjectList,
  getAllThemeList,
  getAllUserList,
  getDataItemList,
  getDomainList,
  getSelectCategoryList
} from '@/api/common'
import { HsAdminBaseScrollSelect } from '@hs-admin/components'
import { Button, Input, Modal } from 'antd'
import { message } from '@hs-admin/utils'
// 新增项目
import EditProjectModal from '@/components/project/edit'

// 用户
export const HsAdminSelectUsers: FC<any> = memo((props: any) => {
  return <HsAdminBaseScrollSelect request={getAllUserList} {...props}></HsAdminBaseScrollSelect>
})

// 项目
export const HsAdminSelectProjects: FC<any> = memo((props: any) => {
  return <HsAdminBaseScrollSelect request={getAllNewProjectList} keywordsKey="search" {...props}></HsAdminBaseScrollSelect>
})

// 项目-带新增
export const HsAdminAddProjects: FC<any> = memo((props: any) => {
  const [isModalOpen, setisModalOpen] = useState(false)
  const [name, setName] = useState('')
  const [finishFlag, setfinishFlag] = useState(false)
  const editRef = useRef<any>(null)

  const addProject = () => {
    editRef.current.showModal()
  }
  const handleOk = () => {
    if (!name) {
      message.warning('项目名称不能为空')
      return
    }
    addNewOrderProject({
      name
    }).then(() => {
      message.success('添加成功')
      handleCancel()
      setfinishFlag(true)
    })
  }
  const handleCancel = () => {
    setName('')
    setisModalOpen(false)
  }

  return (
    <>
      <div className="w-full inline-flex gap-4">
        <HsAdminBaseScrollSelect request={getAllNewProjectList} keywordsKey="search" finishFlag={finishFlag} {...props}></HsAdminBaseScrollSelect>
        <Button type="primary" onClick={addProject}>
          新增项目
        </Button>
      </div>
      <Modal title="新增项目" open={isModalOpen} onOk={handleOk} onCancel={handleCancel}>
        <Input value={name} placeholder="输入项目完整名称" onChange={(e) => setName(e.target.value)} />
      </Modal>
      <EditProjectModal ref={editRef} finish={() => setfinishFlag(true)} />
    </>
  )
})

// 数据主题
export const HsAdminSelectTheme: FC<any> = memo((props: any) => {
  return <HsAdminBaseScrollSelect request={getAllThemeList} {...props}></HsAdminBaseScrollSelect>
})

// 数据项
export const HsAdminSelectDataItem: FC<any> = memo((props: any) => {
  return <HsAdminBaseScrollSelect request={getDataItemList} {...props}></HsAdminBaseScrollSelect>
})

// 领域维护
export const HsAdminSelectDomain: FC<any> = memo((props: any) => {
  return <HsAdminBaseScrollSelect request={getDomainList} {...props}></HsAdminBaseScrollSelect>
})

// 数据产品类别
export const HsAdminSelectProductCategory: FC<any> = memo((props: any) => {
  const { source } = props
  return <HsAdminBaseScrollSelect request={(params) => getSelectCategoryList({ ...params, category: source })} {...props}></HsAdminBaseScrollSelect>
})
